package com.ruoyi.common.utils.getHardwareinfo;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class API {

    private static String TOKEN = "";
    private static final Logger logger = LoggerFactory.getLogger(API.class);

    /**
     * 获取Token
     * */
    public static String getToken() throws IOException {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType,
                    "{\r\n    \"username\": \"cqgczx\"," +
                            "\r\n    \"password\": \"DC483E80A7A0BD9EF71D8CF973673924\"," +
                            "\r\n    \"app_key\":\"c07e9e10b7304753aab1d0b1ae9dc964\"," +
                            "\r\n    \"app_secret\":\"473e833b851a496c9237c9607da2dfb8\"\r\n}");
            Request request = new Request.Builder()
                    .url("https://api.g2.aibabel.cn/babels/token")
                    .method("POST", body)
                    .addHeader("adminBabelWebTokenHeader", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImNvbXBhbnlfaWQiOjEsInVzZXJuYW1lIjoiYWliYWJlbCIsInJvbGUiOjEsIlN0YW5kYXJkQ2xhaW1zIjp7ImlzcyI6ImJhYmVsIiwibmJmIjoxNzIzMDAwMTU3fX0.Zzeh8M6h6yW0qgG8z_Vg_8ZQ8cNq4J5Sfle2b-HfmH8")
                    .addHeader("Content-Type", "application/json")
                    .build();
            //发送请求
            Response response = client.newCall(request).execute();
            if(response.code() == 200){
                //注：response.body().string()在Debugger下watch不到值会报错，必须赋值给一个String对象才能看到。
                String result = response.body().string();
                JSONObject jsonObject = JSONObject.parseObject(result);
                logger.info(jsonObject.toString());
                JSONObject content = jsonObject.getJSONObject("data");
                TOKEN = content.getString("access_token");
            }
            return TOKEN;
    }

    /**
     * 更新设备信息
     * */
    public static void updateDevice(String parameter) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("json",
                        BodyT())
                .build();
        Request request = new Request.Builder()
                .url("https://api.g2.aibabel.cn/babels/device/config/update")
                .method("PUT", body)
                .addHeader("aibabel-access-token2", getToken())
                .addHeader("adminBabelWebTokenHeader", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImNvbXBhbnlfaWQiOjEsInVzZXJuYW1lIjoiYWliYWJlbCIsInJvbGUiOjEsIlN0YW5kYXJkQ2xhaW1zIjp7ImlzcyI6ImJhYmVsIiwibmJmIjoxNzIzMDAwMTU3fX0.Zzeh8M6h6yW0qgG8z_Vg_8ZQ8cNq4J5Sfle2b-HfmH8")
                .build();
        Response response = client.newCall(request).execute();
    }

    /**
     * 设备定时报警
     * */
    public static void policeDevice(String parameter) throws IOException{
        String path = new String();
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String today = year + "-" + month + "-" + day;
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND)+6;
        String time = hour + ":" + minute + ":" + second;

        //获取文件
        if ("报警".equals(parameter)){
            path="校园防欺凌管理系统.wav";
        }
        if ("故障".equals(parameter)) {
            path="程序故障.wav";
        }
        ClassPathResource classPathResource = new ClassPathResource(path);
        File file = classPathResource.getFile();
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("json",
                        "{\"name\":\"1\"," +
                                "\"ids\":[13701]," +
                                "\"is_activation\":true," +
                                "\"binding\":[[\"3号楼\",\"4楼\",13701]]," +
                                "\"date\":\""+today+"\"," +
                                "\"is_loop\":false," +
                                "\"time\":\""+time+"\"," +
                                "\"des\":\"\"}")
                .addFormDataPart("upload",path,
                        RequestBody.create(MediaType.parse("audio/wav"),
                                file))
                .build();
        Request request = new Request.Builder()
                .url("https://api.g2.aibabel.cn/v1/broadcast/add")
                .method("POST", body)
                .addHeader("aibabel-access-token2", getToken())
                .addHeader("adminBabelWebTokenHeader", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImNvbXBhbnlfaWQiOjEsInVzZXJuYW1lIjoiYWliYWJlbCIsInJvbGUiOjEsIlN0YW5kYXJkQ2xhaW1zIjp7ImlzcyI6ImJhYmVsIiwibmJmIjoxNzIzMDAwMTU3fX0.Zzeh8M6h6yW0qgG8z_Vg_8ZQ8cNq4J5Sfle2b-HfmH8")
                .build();
        Response response = client.newCall(request).execute();
    }
    private static String BodyT(){
        return "{\"sn\":\"TSA001242700629\"," +
                "\"ring_volume\":4," +
                "\"police_volume\":4," +
                "\"screen_brightness\":4," +
                "\"police_led\":true," +
                "\"police_sound\":true," +
                "\"sound_led_type\":0," +
                "\"phone_notification\":false," +
                "\"sms_notification\":false," +
                "\"police_audio_url\":\"https://api.g2.aibabel.cn/assets/voice/default.wav\"," +
                "\"app_version\":\"\"," +
                "\"web_rtc_level\":0}";
    }
    private static String BodyF(){
        return "{\"sn\":\"TSA001242700629\"," +
                "\"ring_volume\":4," +
                "\"police_volume\":4," +
                "\"screen_brightness\":4," +
                "\"police_led\":false," +
                "\"police_sound\":true," +
                "\"sound_led_type\":0," +
                "\"phone_notification\":false," +
                "\"sms_notification\":false," +
                "\"police_audio_url\":\"https://api.g2.aibabel.cn/assets/voice/default.wav\"," +
                "\"app_version\":\"\"," +
                "\"web_rtc_level\":0}";
    }
}
